
/**
 * 接听方式选择
 * @param phone 手机号
 * @param id 学员id
 * @returns
 */
function dialingOut(phone,id){
    $("[rel=popover]").popover('destroy');
    let dataContent='<div style="text-align: center;min-width:200px;display: block;">';
    dataContent+='<button type="button" class="btn btn-info" onclick="callOut(\''+phone+'\',1)">手机接听</button>&nbsp;&nbsp;';
    dataContent+='<button type="button" class="btn btn-success" onclick="callOut(\''+phone+'\',2)">企话宝接听</button>';
    dataContent+='</div>';
    dataContent+='<div style="text-align: center;margin-top:5px;display: block;">';
    dataContent+='<button type="button" class="btn btn-default" onclick="$(\'#dialing'+id+'\').popover(\'destroy\');">稍后再拨</button>&nbsp;&nbsp;';
    dataContent+='<div>';
    let dataTitle='<h4>请选择接听方式</h4>';

    $(`#dialing${id}`).popover({
        html : true ,
        content: dataContent,
        title: dataTitle,
        placement: 'auto'
    });
    $("#dialing"+id).popover("show");
}
/**
 * 拨打电话接口
 * @param phone 接听电话
 * @param extenType 拨打方式
 * @returns
 */
function callOut(phone,extenType){
    $.post("/7moor/dialing",{"phone":phone,"type":extenType},function(data){
        //console.log(data);
        if(data.status==0){
            alert(data.errMsg);
        }else{
            $("[rel=popover]").popover('destroy');
            alert("电话已成功拨出，请注意接听");
        }
    },"json");
}
/**
 * 创建发送短信UI
 * @param id 学员id
 * @param phone 短信接收手机
 * @param loginId crm平台登录人id
 */
function sendSms(id,phone,loginId){
    $.post("/student/sms/template/list",{},function(data){
        //console.log(data);
        if(data.status==1){
            $("[rel=popover]").popover('destroy');
            let dataContent='<div id="popoverSms"><div style="text-align: left;min-width:230px;display: block;font-weight: normal;">';
            if(data.data!=null){
                let arrSms=data.data;
                //console.log(arrSms);
                for(let i=0;i<arrSms.length;i++){
                    dataContent+='<label><input type="radio" name="smsId" value="'+arrSms[i].id+'" onclick="$(this).attr(\'checked\',true);createParamsStr('+id+',\''+arrSms[i].content+'\','+loginId+',this)">'+arrSms[i].title+'</label>';
                }

                dataContent+='<p style="width: 100%;"><textarea id="content11111" style="display:none;" rows="4" class="form-control" disabled="disabled"></textarea></p>';
                dataContent+='<p id="remark" style="display:none;color:red;">说明：短信内容中{$var}是按下面输入的内容依次替换。如果名字请输入中文名称</p>';

            }
            dataContent+='</div>';
            dataContent+='<div id="parm" style="text-align: center;display:none;"></div>';

            dataContent+='<div style="text-align: center;margin-top:5px;display: block;">';
            dataContent+='<button type="button" class="btn btn-default" onclick="closePopover(\'sendsms'+id+'\');">再想想</button>&nbsp;&nbsp;';
            dataContent+='<button type="button" class="btn btn-success" onclick="sendSmsSubmit('+id+',\''+phone+'\','+loginId+',this);">发送</button>';
            dataContent+='</div></div>';
            //console.log(dataContent);
            let dataTitle='<h4>请选择发送信息模板</h4>';

            $("#sendsms"+id).popover({
                html : true ,
                title: dataTitle,
                content: dataContent,
                placement: 'auto'
            });
            $("#sendsms"+id).popover("show");
        }else{

        }
    },"json");
}
/**
 * 创建短信中需要替换的内容UI
 * @param id 用户id
 * @param content 短信内容
 * @param loginId  crm平台登录人id
 * @param dom html对象
 */
function createParamsStr(id,content,loginId,dom){
    $('#content11111').show();
    let varNum=content.split("{$var}").length;
    if(varNum>1){
        // 拿到本地存储
        let localData = []
        try {
            let keys = 'sendSms@' + loginId + '@' + $(dom).val()
            if (localStorage.getItem(keys)!=null){
                localData = localStorage.getItem(keys).split(',');
            }
        } catch (e) {
            console.log(e)
        }
        let shtml="";
        if (localData.length > 0) {
            for(let i=0;i<varNum-1;i++){
                shtml+='<div  class="form-inline" style="display: block;width: 100%;padding-left: 0;font-weight: normal;">信息'+(i+1)+'：<input value="'+localData[i+1]+'"  name="paramsStr" class="form-control input-sm" maxlength=11 style="width:170px;"></div>';
            }
        } else {
            for(let i=0;i<varNum-1;i++){
                shtml+='<div  class="form-inline" style="display: block;width: 100%;padding-left: 0;font-weight: normal;">信息'+(i+1)+'：<input  name="paramsStr" class="form-control input-sm" maxlength=11 style="width:170px;"></div>';
            }
        }
        $("#parm").html(shtml);
        $("#remark").show();
        $("#parm").show();
    }else{
        $("#parm").html("");
        $("#parm").hide();
        $("#remark").hide();
    }
    $("#sendsms"+id).attr("data-title","<h4>请选择发送信息模板</h4>");
    $("#sendsms"+id).attr("data-content",$("#popoverSms").html());
    $("#sendsms"+id).attr("data-placement","auto");
    $("#sendsms"+id).attr("data-html","true");
    $("#sendsms"+id).popover('show');
    $('#content11111').val(content);

}
/**
 * 正式发送短信
 * @param id 用户id
 * @param phone 接收短信手机
 * @param loginId crm平台登录人id
 * @param dom 对象
 * @returns {boolean}
 */
function sendSmsSubmit(id,phone,loginId,dom){
    let isSubmit=true;
    let smsId=0;
    let paramsStr=phone;
    if($("input[name='smsId']:checked").length==0){
        isSubmit=false;
        alert("请选择发送短消息模板。");
        return false;
    }else{
        smsId=$("input[name='smsId']:checked").val();
    }
    let inputObj=$("input[name='paramsStr']")
    if(inputObj.length>0){
        inputObj.each(function(){
            if($.trim($(this).val())==""){
                isSubmit=false;
                alert("请输入替换参数。");
                return false;
            }else{
                paramsStr+=","+$.trim($(this).val());
            }
        });
    }

    if(isSubmit){
        // 存储到本地
        try {
            let keys = 'sendSms@' + loginId + '@' + $(dom).parents('.popover-content').find('input[type="radio"]').val()
            let data = localStorage.setItem(keys, paramsStr)
        } catch (e) {
            console.log(e)
        }
        $.post("/student/sms/template/"+smsId,{"paramsStr":paramsStr},function(data){
            if(data.status==1){
                alert("短信发送成功");
            }else{
                alert(data.errMsg);
            }
            closePopover("sendsms"+id);

        },"json");
    }
}

/**
 * 关闭弹窗
 * @param tagId 弹窗对象id
 */
function closePopover(tagId){
    $("#"+tagId).removeAttr("data-title");
    $("#"+tagId).removeAttr("data-content");
    $("#"+tagId).removeAttr("data-placement");
    $("#"+tagId).removeAttr("data-html");
    $("#"+tagId).popover('destroy');
}
